package com.kefu.exam.enums;

import com.kefu.exam.handler.*;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum TipType implements java.io.Serializable{
    Vacancy("1", "填空", new VacancyHandler()),
    // ————————————————————
    Select("2", "单选", new SelectHandler()),
    // ————————————————————
    CheckBox("3", "多选", new CheckBoxHandler()),
    // ————————————————————
    Judge("4", "判断", new JudgeHandler()),
    // ————————————————————
    Interlocution("5", "问答", new InterlocutionHandler()),
    // ————————————————————
    Fileup("6", "附件", new FileUpHandler()),
    // ————————————————————
    Comprehensive("7", "综合", new ComprehensiveHandle());
    /**
     * 题目类型// 1.填空，2.单选，3.多选，4判断，5问答,6附件,7综合
     */
    private String type;

    /**
     * 试题类型的特殊处理逻辑
     */
    private SubjectTipHandler handle;

    private String title;

    TipType(String type, String title, SubjectTipHandler tipHandle) {
        this.type = type;
        this.title = title;
        this.handle = tipHandle;
    }

    public static TipType getTipType(String type) {
        TipType[] tips = TipType.values();
        for (TipType tip : tips) {
            if (tip.getType().equals(type)) {
                return tip;
            }
        }
        throw new RuntimeException("the type is not defind : " + type);
    }

    /**
     * 清空一個題目頭部的數字和點
     *
     * @param text
     * @return
     */
    public static String clearSubjectTextHead(String text) {
        //正则表达式【数字.数字】
        String pattern = "^\\d{0,3}\\.\\d";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(text);
        if (m.find()) {
            //如果疑似小数开头直接返回
            return text;
        } else {
            //如果序号和.之后跟着非数字则判断为题号，直接去掉
            //正则表达式【数字.】
            return text.trim().replaceAll("^\\d{0,3}\\.", "");
        }
    }

    /**
     * 清空一個选项頭部的字母和點
     *
     * @param text
     * @return
     */
    public static String clearOptionTextHead(String text) {
        text=text.trim();
        while (text.length() > 1) {
            if (StringUtils.isAllUpperCase(text.substring(0, 1))) {
                text = text.substring(1);
            } else {
                if (text.length() > 1 && text.substring(0, 1).equals(".")) {
                    text = text.substring(1);
                }
                if (text.length() > 1 && text.substring(0, 1).equals("、")) {
                    text = text.substring(1);
                }
                if (text.length() > 1 && text.substring(0, 1).equals("．")) {
                    text = text.substring(1);
                }
                return text;
            }
        }
        return text;
    }

    public SubjectTipHandler getHandle() {
        return handle;
    }
    public String getType() {
        return type;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public static Map<String, String> getDictionary() {
        TipType[] tips = TipType.values();
        Map<String, String> map = new HashMap<>();
        for (TipType tip : tips) {
            map.put(tip.getType(), tip.getTitle());
        }
        return map;
    }
}
